﻿define(['jquery', 'kendo', 'moment', 'ajax-pre'], function ($, kendo, moment) {

    var userId = "123";

    var certificateType = JSON.parse(document.getElementById('t').value);
    console.log(certificateType);

    var dialog = $('#window');

    dialog.kendoWindow({
        width: "880px",
        title: "证件管理",
        visible: false,
        actions: [
            //"Pin",
            "Minimize",
            "Maximize",
            "Close"
        ],
        //close: onClose
    }).data("kendoWindow").center().open();

    var dataSource = new kendo.data.DataSource({
        schema: {
            data: function (response) {
                return response.Datas || response.Id || response.Data || response;
            },
            model: {
                id: "Id",
                fields: {
                    Id: { nullable: false },
                    Type: { validation: { required: true } },
                    Number: { nullable: false },
                    ValidityStartTime: { validation: { required: true }, defaultValue: new Date() },
                    ValidityEndTime: { validation: { required: true }, defaultValue: new Date() },
                    Other: { validation: { required: false }, defaultValue: null },
                    UserId: { validation: { required: true }, defaultValue: userId },
                }
            }
        },
        serverPaging: false,
        batch: true,
        transport: {
            read: {
                url: "/Certificate",
                dataType: "json",
                method: "post"
            },
            update: {
                url: "/Certificate/Save",
                dataType: "json",
                method: "post",
                contentType: "application/json"
            },
            destroy: {
                url: "/Certificate/Remove",
                dataType: "json",
                method: "post",
                contentType: "application/json"
            },
            create: {
                url: "/Certificate/Save",
                dataType: "json",
                method: "post",
                contentType: "application/json"
            },
            parameterMap: function (options, operation) {
                if (operation !== "read" && options.models) {
                    return JSON.stringify({ certificates: options.models });
                }
                if (operation == "read") {
                    return {
                        filter: {
                            userId: userId
                        }
                    };
                }
            }
        }
    });

    $("#grid").kendoGrid({
        dataSource: dataSource,
        filterable: true,
        toolbar: [{ name: "create", text: '新建' }, { name: "save", text: '保存' }],
        pageable: {
            refresh: true,
            pageSizes: true,
            buttonCount: 5
        },
        columns: [{
            field: "Type", title: "证件类型", width: '180px',
            editor: function (container, options) {


                $('<input data-bind="value:' + options.field + '" />').appendTo(container)
                .kendoComboBox({
                    //placeholder: "顶级部门",
                    dataTextField: "Name",
                    dataValueField: "Value",
                    dataSource: certificateType
                });
            }
        },
        {
            field: "Number",
            title: "证件号",
            width: '160px'

        },
         {
             field: "ValidityStartTime", title: "有效期开始", width: '150px',
             template: function (value) {
                 return new moment(value.ValidityStartTime).format('YYYY-MM-DD');
             },
             editor: function (container, options) {
                 $('<input data-bind="value:' + options.field + '" />').appendTo(container)
                 .kendoDatePicker({
                     format: 'yyyy-MM-dd',
                     interval: 1
                 });
             }
         },
         {
             field: "ValidityEndTime", title: "有效期结束", width: '150px',
             template: function (value) {
                 return new moment(value.ValidityEndTime).format('YYYY-MM-DD');
             },
             editor: function (container, options) {
                 $('<input data-bind="value:' + options.field + '" />').appendTo(container)
                 .kendoDatePicker({
                     format: 'yyyy-MM-dd',
                     interval: 1
                 });
             }
         },
         {
             field: "Other", title: "其他", width: '150px',
             //template: function (value) {
             //    return new moment(value.ValidityEndTime).format('YYYY-MM-DD');
             //},
         },
        {
            command: [
              { name: "destroy", 'text': '删除' },
              { name: "edit", text: '编辑' }
            ]
        }
        ],
        editable: { //disables the deletion functionality
            update: true,
            destroy: true,
            create: true,
            mode: 'inline',// inline popup
        },
        //editable: "popup",
        pageable: false,

    });
});